package org.teamh.connection.json.handler;

import org.teamh.connection.json.event.JsonEventObject;
import org.teamh.game.Start;
import org.teamh.gui.Event;
import org.teamh.logic.datamodel.*;

public class GameHandler extends JsonMasterHandler {

	public GameHandler(Start start, DatamodelManager manager) {
		super(start, manager);
	}

	public boolean handleMsg(JsonEventObject json) {
	
		if (json.getSource().startsWith("CreepSmashGame")) {
			if (json.getProperty().equals("guide")) {
				String gameID = json.getSource();
				String guideID = json.getNewValue();	
				CreepSmashGame game = new CreepSmashGame(gameID);
				Guide guide = new Guide(guideID);
				start.getLogic().setGame(game);
				game.setGuide(guide);
				return true;
			} else if (json.getProperty().equals("name")) {
				start.getLogic().getGame().setName(json.getNewValue());
				return true;
			} else if (json.getProperty().equals("numPlayers")) {
				start.getLogic().getGame().setNumPlayers(Integer.parseInt(json.getNewValue()));
				return true;
			} else if (json.getProperty().equals("running")) {
				start.getLogic().getGame().setRunning(Boolean.parseBoolean(json.getNewValue()));
				if (json.getNewValue().equals("true")) {
					start.getGui().addEvent(new Event("running", ""));
				}
				return true;
			} else if (json.getProperty().equals("testGame")) {
				start.getLogic().getGame().setTestGame(Boolean.parseBoolean(json.getNewValue()));
				return true;
			} else if (json.getProperty().equals("tiles")) {
				if (manager.getTile(json.getNewValue()) == null) {
					String tileID = json.getNewValue();
					Tile tile = new Tile(tileID);
					start.getLogic().getGame().addTile(tile);
					manager.addTile(tile);
				}
				return true;
			} else if (json.getProperty().equals("winner")) {
				if (json.getNewValue() != null) {
					User user = manager.getUser(json.getNewValue());
					
					if(user != null) {
						start.getLogic().getGame().setWinner(user);
						start.getGui().addEvent(new Event("winner", user.getUserAssets().getTile().getPosition()));
					} else {
						System.out.println("An error while setting the winner occured");
					}
				}
				return true;
			} else if (json.getProperty().equals("nextIncome")) {
				start.getLogic().getGame().setNextIncome(Long.parseLong(json.getNewValue()));
				return true;
			} else if (json.getProperty().equals("users")) {
				CreepSmashGame creepSmashGame = start.getLogic().getGame();
				
				if(json.getNewValue() != null) {
					UserAssets userAssets = manager.getUserAssets(json.getNewValue());
					if(userAssets == null) {
						userAssets = new UserAssets(json.getNewValue());
						manager.addUserAssets(userAssets);
					}
					creepSmashGame.addUserAssets(userAssets);
				} else {
					UserAssets userAssets = manager.getUserAssets(json.getOldValue());
					if(userAssets != null) {
						creepSmashGame.removeUserAssets(userAssets);
						manager.removeUserAssets(userAssets.getID());
					}
				}
				return true;
			} else if (json.getProperty().equals("speed")) {
				start.getLogic().getGame().setSpeed(Double.parseDouble(json.getNewValue()));
				start.getModel().setManagerReady(true);
				return true;
			}
			return false;
		} else {
			return false;
		}
	}
}
